package com.agileai.crm.module.mytasks.handler;

import java.math.BigDecimal;
import java.util.Date;

import com.agileai.common.KeyGenerator;
import com.agileai.crm.cxmodule.FollowUpManage;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

/**
 * generated by miscdp
 */
public class FollowUpManageHandler extends SimpleHandler{
	public FollowUpManageHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		User user = (User) getUser();
		param.put("VISIT_USER_ID", user.getUserId());
		param.put("VISIT_USER_NAME", user.getUserName());
		param.put("VISIT_FILL_ID", user.getUserId());
		param.put("VISIT_FILL_NAME", user.getUserName());
		String date = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL, new Date());
		param.put("VISIT_DATE", date);
		param.put("VISIT_FILL_TIME", date);
		this.setAttributes(param);
		this.processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	
	protected void processPageAttributes(DataParam param){
		setAttribute("VISIT_TYPE", FormSelectFactory.create("VISIT_TYPE")
				.addSelectedValue(getOperaAttributeValue("VISIT_TYPE", "")));
		setAttribute("VISIT_EFFECT", FormSelectFactory.create("VISIT_EFFECT")
				.addSelectedValue(getOperaAttributeValue("VISIT_EFFECT", "")));
		setAttribute(
				"VISIT_RECEPTION_SEX",
				FormSelectFactory.create("USER_SEX").addSelectedValue(
						getOperaAttributeValue("VISIT_RECEPTION_SEX", "M")));
		setAttribute(
				"VISIT_STATE",
				FormSelectFactory.create("CUST_STATE").addSelectedValue(
						getAttributeValue("VISIT_STATE", "init")));
	}	
	
	public ViewRenderer doSaveAction(DataParam param){
		String rspText = FAIL;
		BigDecimal orderMoney = new BigDecimal("0.00");
		if(param.get("VISIT_COST").trim().equals("")){
			param.put("VISIT_COST",orderMoney);
		}
		
		DataRow taskRow = getService().getMasterRecord(param);
		String taskFollowState = taskRow.getString("TASK_FOLLOW_STATE");
    	if("Init".equals(taskFollowState)){
    		param.put("TASK_FOLLOW_STATE", "HaveFollowUpInit");
    	}else{
    		param.put("TASK_FOLLOW_STATE", "HaveFollowUp");
    	}
		
		getService().updateTaskStateRecord(param);
		param.put("TASK_CLASS", "FollowUp");
		getService().updateTaskClassRecord(param);
		
		String custId = param.get("VISIT_CUST_ID");
		
		if(StringUtil.isNotNullNotEmpty(custId)){
			DataParam custParam = new DataParam();
			custParam.put("CUST_ID", custId);
			custParam.put("CUST_STATE", "init");
			getService().updateCustStateRecord(custParam);
			param.put("VISIT_CUST_ID", param.get("custId"));
			getService().createCustVisitRecord(param);
		}else{
			String custUuid = KeyGenerator.instance().genKey();
			param.put("custUuid", custUuid);
			getService().createCustRecord(param);
			param.put("VISIT_CUST_ID", param.get("custUuid"));
			getService().createCustVisitRecord(param);
		}
		
		rspText = SUCCESS;
		return new AjaxRenderer(rspText);
	}
	
    protected FollowUpManage getService() {
        return (FollowUpManage) this.lookupService(FollowUpManage.class);
    }
}
